package ua.ck.example.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import ua.ck.example.domain.Customer;

@Repository
public class CustomerDAOImpl implements CustomerDAO {

	@Autowired
	private SessionFactory sessionFactory;


	@Override
	public void addCustomer(Customer customer) {
		sessionFactory.getCurrentSession().save(customer);

	}
	
	@Override
	public Customer getCustomer(Customer customer){
	 StringBuffer sql = new StringBuffer("FROM Customer c where 1=1");
		sql.append(" and c.customerName='" + customer.getCustomerName()+"'");
		sql.append(" and c.password='" + customer.getPassword()+"'");
		
		
		List<Customer> customerList = (List<Customer>) sessionFactory
				.getCurrentSession().createQuery(sql.toString()).list();
		if (customerList != null && customerList.size() > 0) {
			return (Customer)customerList.get(0);
		}
		return null;
	}
	
}
